package us.mooresr.aircraftscheduling.server.handler;



import java.util.ArrayList;
import java.util.HashMap;

import us.mooresr.aircraftscheduling.server.DAO;
import us.mooresr.aircraftscheduling.shared.action.RetrieveAllOpenReservationsResult;
import us.mooresr.aircraftscheduling.shared.action.RetrieveReservationAction;
import us.mooresr.aircraftscheduling.shared.action.RetrieveReservationResult;
import us.mooresr.aircraftscheduling.shared.model.Reservation;
import us.mooresr.aircraftscheduling.shared.model.User;

import com.googlecode.objectify.Query;
import com.gwtplatform.dispatch.server.ExecutionContext;
import com.gwtplatform.dispatch.server.actionhandler.ActionHandler;
import com.gwtplatform.dispatch.shared.ActionException;

public class RetrieveReservationHandler 
	implements ActionHandler<RetrieveReservationAction, RetrieveReservationResult> {

	@Override
	public RetrieveReservationResult execute(RetrieveReservationAction arg0,
			ExecutionContext arg1) throws ActionException {
		DAO dao = new DAO();
		
		Reservation result = dao.ofy().query(Reservation.class).filter("reservationId", arg0.getReservationId()).get();
		
		if (result != null) {
			User user = dao.ofy().query(User.class).filter("userId", result.getReservingUserId()).get();
			result.setReservingUserName(user.getName());
		}
		
		return new RetrieveReservationResult(result);
		
		
	}

	@Override
	public Class<RetrieveReservationAction> getActionType() {
		return RetrieveReservationAction.class;
	}

	@Override
	public void undo(RetrieveReservationAction arg0,
			RetrieveReservationResult arg1, ExecutionContext arg2)
			throws ActionException {
		
	}

}
